<?php
$error_message = "";
if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $username)) {
        $error_message = 'Enter valid username *.<br />';
    }
    $pswd_exp = "/^[A-Za-z0-9 .'-]+$/";
    if (!preg_match($pswd_exp, $password)) {
        $error_message = 'Enter valid password *.<br />';
    }
    if (strlen($error_message) == 0) {
        try {
            $conn = mysql_connect('localhost', 'root', '');
            if (!$conn) {
                die("Could not connect :" . mysql_error());
            } else {
                mysql_select_db("j2eedevelopers", $conn);
                $cdt = date("d/m/y : H:i:s", time());
                //echo "connected successfully";
                //echo ("INSERT INTO ADMIN (USERNAME,PASSWORD) VALUES('$username','$password')");
                mysql_query("INSERT INTO admin (cdt, username,password) VALUES('$cdt', '$username','$password')");
                //echo 'Query successfully';
                mysql_close($conn);
            }
        } catch (Exception $e) {
            die('Could not insert :' . $e->getMessage());
        }
        if (empty($error_message)) {
            header("location:adminreports.php");
        }
    }
}
?>

<html>
    <head>
        <title>Admin page</title>
    </head>
    <body bgcolor="99CCFF" style="color:#ffffff"/>
    <h3>  <p align="center" >J2EEDEVELOPER/ADMIN LOGIN</p></h3>
    <p align="center"><font face="Arial" size="2">Please login using the username 
        and password provided to you.</font></p>
    <div align="center"/>
    <table border="0" width="60%" id="table1" cellspacing="0" cellpadding="0"/>
    <tr>
        <td span="2" style="padding-left: 360px;color:red;"><?php echo $error_message ?>
        </td>
    </tr>
</table>      
<form method="post">
    Username*:<input type="text" name="username" value=""/><br/>
    Password*:<input type="password" name="password" value=""/><br/>
    <input type="submit" name="Submit" value="Submit"/></form> 
</body>
</html>

